package org.tysite.tyservice.example.restful.controller;

import org.springframework.web.bind.annotation.*;
import org.tysite.tyservice.example.restful.dto.BowDTO;

import java.util.HashMap;
import java.util.Map;

/**
 * spring mvc 参数接收演示代码
 * @author tysite
 * @date 2020/2/29 0029 16:21
 */
@RestController
@RequestMapping("/api/example/restful")
public class RestfulController {
    
    /**
     * 演示通过 form-data 提交参数方案的参数接收。
     * @param path         路径片段
     * @param key          请求头中的秘钥属性
     * @param cookie       cookie值
     * @param poundage     获取参数中的数组，其同时支持从Query parameters中获取
     * @param param        从form-data提交中绑定model属性
     * @return             返回map对象
     */
    @PostMapping("/{path}")
    public Map demoReceiveParams(@PathVariable String path,
                                 @RequestHeader(name = "secret-key", required = false) String key,
                                 @CookieValue(name = "cookie", required = false) String cookie,
                                 @RequestParam(value = "poundage", required = false) String[] poundage,
                                 BowDTO param
                                 ) {
        Map<String,Object> result = new HashMap<>(16);
        result.put("path segments", path);
        result.put("secret-key", key);
        result.put("cookie", cookie);
        result.put("request param", poundage);
        result.put("body model", param);
        return result;
    }
    
    /**
     * 演示 application/json媒体类型请求体接收参数
     * @param param    json格式的请求参数
     * @return         返回map对象
     */
    @PostMapping("/request-body")
    public Map demoRequestBody(@RequestBody BowDTO param) {
        Map<String,Object> result = new HashMap<>(16);
        result.put("model", param);
        return result;
    }
}